package com.ten.course.utils;

import com.ten.common.core.entity.EduSubject;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author ：张成（3092211427@qq.com）
 * @date ：Created in 2022/12/9 16:29
 * @description： 课程工具类
 * @modified By：
 * @version:
 */
public class CourseUtil {
    //将获取到的菜单权限进行层级的分配
    public static List<EduSubject> fromEduSubjectListToTreeEduSubjectList(List<EduSubject> eduSubjects) {
        List<EduSubject> treeList =new ArrayList<>();
        for (EduSubject eduSubject : eduSubjects) {
            //pid为1，为一级菜单
            if(eduSubject.getParentId().equals("0")){
                //设置子菜单（其中包含二级菜单等下面不知层级数的菜单，采用递归调用）
                //得到一级菜单包括一级菜单下的子菜单
                EduSubject childrenPermission = setTreeChildren(eduSubject,eduSubjects);
                treeList.add(childrenPermission);
            }
        }
        return treeList;
    }

    public static EduSubject setTreeChildren(EduSubject eduSubject, List<EduSubject> eduSubjects) {
        List<EduSubject> childrenList = new ArrayList<>();
        for (EduSubject subject : eduSubjects) {
            if(Objects.equals(subject.getParentId(), eduSubject.getId())){
                //继续调用设置子菜单
                EduSubject treeChildren = setTreeChildren(subject, eduSubjects);
                childrenList.add(treeChildren);
            }
        }
        //将封装好的数据设置到子菜单中
        eduSubject.setChildren(childrenList);
        return eduSubject;
    }

    public static void main(String[] args) {
        List<Integer> list =new ArrayList<>();
        list.add(1);
        list.add(2);
        getOne(list);
        for (Integer integer : list) {
            System.out.println(integer);
        }

    }

    //将获取到的分类进行层级的分配
    public static List<List<EduSubject>> getAllSubjectsFromSubject(List<EduSubject> eduSubjects) {
        List<List<EduSubject>> listList = new ArrayList<>();
        for (EduSubject eduSubject : eduSubjects) {
            //pid为0，为一级菜单
            List<EduSubject> list= null;
            if (eduSubject.getParentId().equals("0")) {
                //获取到一级课程分类下的所有孩子到list中
                list = getTreeChildrenToList(eduSubject, eduSubjects);
            }
            listList.add(list);
        }
        return listList;
    }

    //eduSubject 任何一个一级
    public static List<EduSubject> getTreeChildrenToList(EduSubject eduSubject, List<EduSubject> eduSubjects) {
        List<EduSubject> toList = new ArrayList<>();
        for (EduSubject subject : eduSubjects) {
            if (Objects.equals(subject.getParentId(), eduSubject.getId())) {
                if(subject.getChildren()!=null){
                    //继续调用设置子菜单
                    toList = getTreeChildrenToList(subject, eduSubjects);
                }
                toList.add(subject);
            }
        }
        return toList;
    }

    private static void getOne(List<Integer> list) {
        list.add(3);
    }
}